import { WidgetBaseTextSession } from './base'
import {toAny, BoolValue, fromAny} from '../../proto'
export class WidgetCheckBoxSession extends WidgetBaseTextSession {
    private checkBox: HTMLInputElement;
    private label: HTMLElement
    constructor(render: HTMLElement) {
        super('CheckBox', render)
    }
    createWidget(): HTMLElement {
        this.checkBox = document.createElement('input')
        this.checkBox.type = 'checkbox'
        this.label = document.createElement('label')
        return this.label
    }
    onCreate(sessionId: number, futureId: number, ...argv) {
        super.onCreate(sessionId, futureId, ...argv);
        this.label.prepend(this.checkBox)
    }
    onDestroy() {
        this.checkBox.remove()
        super.onDestroy();
    }
    onReceive(name: String, sessionId: number, futureId: number, ...argv): void {
        switch (name) {
            case 'isChecked':
                return this.isChecked(sessionId, futureId)
            case 'setChecked':
                return this.setChecked(...argv)
        }
        super.onReceive(name, sessionId, futureId, ...argv);
    }
    private setChecked(...argv) {
        this.checkBox.checked = fromAny(argv[0], BoolValue)
    }
    private isChecked(sessionId: number, futureId: number) {
        const checked = this.checkBox.checked
        this.sendAction("onIsChecked", sessionId, futureId, toAny(checked, BoolValue))
    }
}